﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BadHabit.WPFTools {

    public class DelayFilter {

        public delegate void ApplyFilterDelegate();
        private ApplyFilterDelegate applyFilterMethod;

        private System.Windows.Threading.DispatcherTimer searchTimer = new System.Windows.Threading.DispatcherTimer();

        public DelayFilter(int seconds, ApplyFilterDelegate applyFilterMethod) {
            this.applyFilterMethod = applyFilterMethod;
            searchTimer.Interval = new TimeSpan(0, 0, 1);
            searchTimer.Tick += new EventHandler(searchTimer_Tick);

        }

        public void StartFilter() {
            if (!searchTimer.IsEnabled) {
                searchTimer.Start();
            }
        }

        private void searchTimer_Tick(object sender, EventArgs e) {            
            applyFilterMethod();
            searchTimer.Stop();
        }
    }
}
